﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog.bbsmanage
{
    public partial class banzhu : System.Web.UI.Page
    {
        string ParentID = "1";
        cbcata c;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                string sql = " select 省编码 from 省 ";
                DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("");
                foreach (DataRow item in dt.Rows)
                {
                    DropDownList1.Items.Add(item[0].ToString());
                }
                BindTree();
            }
        }
        private void BindTree()
        {
            cbarea area;
            area = cbarea.Find(" cbuserid = '" + AliApi.appUserId + "'");
            if (area.Count < 1)
            {
                MessageBox.Show(this.Page, "请先创建站点.");
                Response.Redirect(ResolveUrl("~/bbsmanage/newsiteapp.aspx"));
            }
            ParentID = area.cbcataid.ToString();
            TreeView1.Nodes.Clear();
            c = cbcata.Find("( cbstatus = '" + area.cbareaid.ToString()
                + "')");
            Label_id.Text = c.cbcataid.ToString();
            Label_name.Text = c.cbname.ToString();
            BindGrid(Label_id.Text);
            string filter = " cbcataid ='" + ParentID
                + "'";
            DataRow[] dRows = c.Select(filter);
            foreach (DataRow dRow in dRows)
            {
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["cbcataid"].ToString();
                childNode.Text = dRow["cbcataid"].ToString() + " " + dRow["cbname"].ToString();
                int childCount = BindTreeNode(childNode.ChildNodes, dRow["cbcataid"].ToString());
                if (childCount > 0)
                    TreeView1.Nodes.AddAt(0, childNode);
                else
                    TreeView1.Nodes.Add(childNode);
            }
            TreeView1.ExpandAll();
        }

        private int BindTreeNode(TreeNodeCollection nodeCollection, string parentNodeId)
        {
            string filter = " pid1='" + parentNodeId
                + "'";
            DataRow[] dRows = c.Select(filter);
            int count = 0;

            foreach (DataRow dRow in dRows)
            {
                count++;
                TreeNode childNode;
                childNode = new TreeNode();
                childNode.Value = dRow["cbcataid"].ToString();
                childNode.Text = dRow["cbcataid"].ToString() + " " + dRow["cbname"].ToString();

                int childCount = BindTreeNode(childNode.ChildNodes, dRow["cbcataid"].ToString());
                if (childCount > 0)
                    nodeCollection.AddAt(0, childNode);
                else
                    nodeCollection.Add(childNode);
            }
            return count;
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.TreeView1.SelectedNode != null)
            {
                cbcata c = cbcata.Find(" cbcataid = '" + this.TreeView1.SelectedNode.Value + "'");
                Label_id.Text = c.cbcataid.ToString();
                Label_name.Text = c.cbname.ToString();
                BindGrid(Label_id.Text);
            }
        }

        private void BindGrid(string cataid)
        {
            cbbanzhu bz = cbbanzhu.Find(" cbcataid = '" + cataid + "'");
            GridView_banzhu.DataSource = bz.MyTable;
            GridView_banzhu.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            cbbanzhu bz = cbbanzhu.Find(" 1=2 ");
            bz.cbuserid.Value = TextBox_name.Text;
            bz.cbcataid.Value = Label_id.Text;
            bz.area.Value = DropDownList1.Text;
            bz.Insert();
            BindGrid(Label_id.Text);
        }

        protected void GridView_banzhu_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView_banzhu.DataKeys[e.RowIndex].Value.ToString();
            cbbanzhu uc = cbbanzhu.Find(" recid = '" + id + "'", "RECID");
            uc.Delete();
            BindGrid(Label_id.Text);
        }
    }
}
